<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
// register all resources of app
use App\Services\MenuService;

const RESOURCES = [
    'admins' => 'Admin',
    'companies' => 'Company',
    'wxclients' => 'Wxclient',
    'simCards' => 'SimCard',
    'statistics' => 'Statistics',
];

Route::group([
    'prefix' => 'v1',
    'middleware' => ['auth', 'json']
], function () {
    Route::get('/wxclients/{wxClientId}/loginStatus', 'Resources\WxclientController@refresh');
    Route::get('/statistics', 'Resources\StatisticsController@index');
    Route::get('/statistics/export', 'Resources\StatisticsController@export');
    Route::get('/wxclients/{wxClientId}/remove', 'Resources\WxclientController@remove');
    Route::put('/wxclients/{wxClientId}/clearUin', 'Resources\WxclientController@clearUin');
    Route::get('/simCards/indexes/{indexName}/{mbImprove?}', 'Resources\SimCardController@index');
    Route::post('/simCards/import', 'Resources\SimCardController@import');
    Route::post('/wxclients/import', 'Resources\WxclientController@import');
    Route::get('/simCards/export', 'Resources\SimCardController@export');
    Route::get('/wxclients/export', 'Resources\WxclientController@downFileMould');
    Route::post('/api/upload', 'ApiController@upload');
    Route::get('/api/qrcode/{wxClientId}', 'ApiController@qrcode');
    Route::get('/api/loginQrcode/{uuid}', 'ApiController@show_qrcode');
    Route::options('/companies/{id}', 'Resources\CompanyController@option');
    // 重置密码接口
    Route::patch('/companies/{company_id}/rpwd', 'Resources\CompanyController@resetPassword');
    Route::get('/projects', 'ApiController@projects');
    Route::post('/auth/password/modify', 'Auth\PasswordController@changePassword');
    Route::group(['namespace' => 'WxApi','middleware' => ['auth']], function(){
        Route::any('/mapp/{action}/{param?}', function($action, $param = null) {
            $wxController = new App\Http\Controllers\WxApi\IndexController();
            if (method_exists($wxController, $action)) {
                return call_user_func([$wxController, $action], $param);
            }
        });
    });
});

Route::group(['middleware' => ['web', 'auth']], function () {
    // handle some static or single html view
//    Route::get('{view}.html', function($_view) {
//        $viewController = new App\Http\Controllers\ViewController();
//        if (method_exists($viewController, $_view)) {
//            return call_user_func([$viewController, $_view]);
//        }
//        abort(404);
//    });
    Route::group([
        'prefix' => 'v1',
        'middleware' => ['json'],
    ], function() {
        foreach (RESOURCES as $k => $item) {
            $controller_name = 'Resources\\'.$item.'Controller';
            Route::resource(
                $k,
                $controller_name,
                [ 'except' => ['create', 'edit'], ]
            );
        }
        // 注册特殊的关系型资源
        Route::resource('companies/{company?}/wxclients', CompanyWxclientController::class);
    });
    MenuService::registerRoutes();
});

Route::auth();
Route::get('/logout', 'Auth\LoginController@logout');
Route::get('/', 'HomeController@index');
Route::get('/home', 'HomeController@index');
